<?php
/**
 * 获取数据
 *
 * @category
 * @package
 * @author liuzecheng<liuzecheng@shopex.cn>
 * @version $Id: Z
 */
class ome_event_trigger_shop_data_delivery_vop extends ome_event_trigger_shop_data_delivery_common
{
    public function get_sdf($delivery_id)
    {
        $this->__sdf = parent::get_sdf($delivery_id);

        if ($this->__sdf) {
            $this->_get_order_all_objects_sdf($delivery_id);
        //    $this->_get_split_sdf($delivery_id);
           $order = $this->__delivery_orders[$delivery_id];
            $delete_flag = false;
            $sql = "SELECT d.logi_no,d.delivery_id
                FROM sdb_ome_delivery_order AS do
                LEFT JOIN sdb_ome_delivery AS d
                ON(do.delivery_id=d.delivery_id)
                WHERE do.order_id='".$order['order_id']."' AND d.parent_id='0' AND d.disabled='false' AND d.status IN('succ')";
        	$delivery_list = kernel::database()->select($sql);
                $itemsRepObj = app::get('ome')->model('order_itemsRep');
                $itemsRep_list = $itemsRepObj->getlist('*',array('order_id'=>$order['order_id']));
                $rep_shop_goods_id = array();
                foreach($itemsRep_list as $items){
                   $itemsRep_list[$items['newitem_id']] = array('shop_goods_id'=>$items['shop_goods_id'],'amount'=>$items['nums']);
                }

    	        if (count($delivery_list)>1){
        		$package_list = array();
        		$delete_exist = true;
                        $bind_data = array();
                        $order_objects = $this->_get_order_objects($delivery_id);
                        
                        foreach ($order_objects as $object) {
                           if (!$object['oid'] && $object['shop_goods_id'] != '-1') {
                                $delete_exist = false;
                           }
                           if ($object['shop_goods_id'] && $object['shop_goods_id'] != '-1') {
                               $order_shop_goods_id[$object['obj_id']] = $object;

                           }
                        }

                        
                        $item_has = array();
	        	foreach($delivery_list as $v){
                                $deliveryItemDetailModel = app::get('ome')->model('delivery_items_detail');
        			$delivery_items_detail = $deliveryItemDetailModel->getList('*',array('delivery_id'=>$v['delivery_id']));

			                
	        		$product_list = array();

              	                foreach ($delivery_items_detail as $key => $value) {
			         
                                    if ($itemsRep_list[$value['order_item_id']]){
                                       if (!$item_has[$value['order_item_id']]){
                                       $item_has[$value['order_item_id']] = $value['order_item_id'];
                                       $barcode = $itemsRep_list[$value['order_item_id']]['shop_goods_id'];
                                       $amount = $itemsRep_list[$value['order_item_id']]['amount'];
                                       }
                                    }else{
                                       $barcode = $order_shop_goods_id[$value['order_obj_id']]['shop_goods_id'];
                                       $amount = $value['number'];
                                    }
                                    if ($barcode){

                                  if ($bind_data[$barcode] ){

                                                                if($bind_data[$barcode]['number']<$order_shop_goods_id[$value['order_obj_id']]['quantity']){
								$product_list[] = array('barcode'=>$barcode,'amount'=>$amount);
								$bind_data[$barcode]['number'] = kernel::single('eccommon_math')->number_plus(array($bind_data[$barcode]['number'],$amount));
                                                                }
							}else{
								$bind_data[$barcode]['number'] = $amount;
								$product_list[] = array('barcode'=>$barcode,'amount'=>$amount);
							}
                                    } 
	        		}
                
                if ($product_list){
	        		  $package_list[] = array('package_product_list'=>$product_list,'transport_no'=>$v['logi_no']);
                                }
	        	}

	        	if ($package_list && $delete_exist){
	        		$this->__sdf['package_list'] = $package_list;
	        	}
        	}


        }
        return $this->__sdf;
    }
}
